#include "SQList.h"
void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);//ps->a是动态分配的，一定要释放掉，避免内存泄漏
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)//这里就涵盖了两种情况：1.列表非空但已满，2.列表为空
	{
		int capacity_tmp;
		if (ps->capacity == 0)
		{
			capacity_tmp = INIT_CAPACITY;
		}
		else
		{
			capacity_tmp = 2 * ps->capacity;
		}
		SLDataType* a_tmp = (SLDataType*)realloc(ps->a, capacity_tmp * sizeof(SLDataType));//避免出现分配失败，原指针丢失的情况
		if (a_tmp)
		{
			ps->a = a_tmp;
			ps->capacity = capacity_tmp;
		}
		else
		{
			perror("申请失败");
			exit(1);
		}
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}